package com.xkdx.caipiao;

import java.text.DecimalFormat;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.unionpay.UPPayAssistEx;
import com.unionpay.uppay.PayActivity;
import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.fragment.UserInfoFragment;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.recharge.RechargeAction;
import com.xkdx.caipiao.presistence.recharge.RechargeModule;
import com.xkdx.caipiao.presistence.recharge.RechargePresistence;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;

public class RechargeActivity extends ThreadActivity implements OnClickListener {

	static String UPMPAY = "upmppay";
	static String ALIPAY = "alipay";
	RechargeAction action;
	RechargeModule module;
	RechargePresistence presistence;
	Button btn_sure_pay;
	ImageView upmpay, alipay;
	static String payway = "alipay";
	Button btn_five, btn_ten, btn_thiry, btn_fifty, btn_hundred,
			btn_twohundred, btn_threehundred, btn_fivehundred;

	int chooseButton = R.id.five;
	int money = 500;
	EditText et_recharge;
	TextView balance;
	String submoney = "0";
	LinearLayout ll;
	private float mon3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge_caipiao);
		InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		LoginInfo info = getLoginInfo();
		submoney = info.getMoney();
//		submoney = getIntent().getStringExtra("money");
		if(null != submoney){
			float money = Float.valueOf(submoney)/ 100;;
			DecimalFormat df = new DecimalFormat("#0.00");
			submoney = df.format(money);
		}
		init();
		setClick();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
	          //记住这个按键
	    }
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (MyApplication.isClosePayDetail) {
			MyApplication.isClosePayDetail = false;
			finish();
		}
	}

	public void onDestroy(){
		super.onDestroy();
		MyApplication.getInstance().isPaying = false;
	}
	
	private void init() {
		btn_sure_pay = (Button) findViewById(R.id.sure_pay);
		balance = (TextView) findViewById(R.id.balance);
		if ("0.00".equals(submoney)) {
			balance.setText("0");
		}else {
			balance.setText(submoney);
		}
		upmpay = (ImageView) findViewById(R.id.upmppay);
		alipay = (ImageView) findViewById(R.id.alipay);
		alipay.setImageResource(R.drawable.bg_bank_selected_caipiao);

		et_recharge = (EditText) findViewById(R.id.recharge_money);

		btn_five = (Button) findViewById(R.id.five);
		btn_ten = (Button) findViewById(R.id.ten);
		btn_thiry = (Button) findViewById(R.id.thirty);
		btn_fifty = (Button) findViewById(R.id.fifty);
		btn_hundred = (Button) findViewById(R.id.hundred);
		btn_twohundred = (Button) findViewById(R.id.twohundred);
		btn_threehundred = (Button) findViewById(R.id.threehundred);
		btn_fivehundred = (Button) findViewById(R.id.fivehundred);

	}

	private void setClick() {
		btn_sure_pay.setOnClickListener(this);
		upmpay.setOnClickListener(this);
		alipay.setOnClickListener(this);
		btn_five.setOnClickListener(this);
		btn_ten.setOnClickListener(this);
		btn_thiry.setOnClickListener(this);
		btn_fifty.setOnClickListener(this);
		btn_hundred.setOnClickListener(this);
		btn_twohundred.setOnClickListener(this);
		btn_threehundred.setOnClickListener(this);
		btn_fivehundred.setOnClickListener(this);
		et_recharge.setOnClickListener(this);

	}

	public void onBackClick(View view) {
		finish();
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			UserInfoFragment.isUpdate = true;
			IConstants.localUserMoney = 0;
			if (payway.equals(ALIPAY)) {
				if (module.url != null) {
					Intent intent = new Intent(this, WebActivity.class);
					intent.putExtra("url", module.url);
					intent.putExtra("title", "支付宝");
					startActivity(intent);

				}
			} else if (payway.equals(UPMPAY)) {
				if (module.tn != null) {
					String serverMode = "00";
					int ret = UPPayAssistEx.startPay(this, null, null,
							module.tn, serverMode);
					if (ret == UPPayAssistEx.PLUGIN_NOT_FOUND) {
						UPPayAssistEx.installUPPayPlugin(this);
					}
				}
			}

		}

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		// TODO Auto-generated method stub
		if (data == null) {
			return;
		}
		String str = data.getExtras().getString("pay_result");
		Log.i("hz", "pay_result" + str);
		if (str.equalsIgnoreCase("")) {

		}
		finish();
		MyApplication.isClosePacketList = true;
		MyApplication.isClosePayDetail = true;
		MyApplication.isOrderPay = true;
		MyApplication.isDetails = true;

	}

	@Override
	public void startThread() {

		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		int et_money;
		if (!et_recharge.getText().equals("")) {
			et_money = Integer.valueOf(et_recharge.getText().toString()) * 100;

		} else {
			et_money = 0;
		}

		if (et_money > 0) {
			LoginInfo info = getLoginInfo();
			String md5 = info.getId()+info.getLogintoken()+IConstants.key;
			String signcode = Md5Utils.md5(md5);
			action = new RechargeAction(info.getId(), info.getLogintoken(),
					payway, et_money + "",signcode);
			module = new RechargeModule();
			presistence = new RechargePresistence(this);
			presistence.setActitons(action);
			presistence.setModule(module);
			presistence.execute();
		} else {
			if (et_money == 0 && money != 0) {
				LoginInfo info = getLoginInfo();
				String md5 = info.getId()+info.getLogintoken()+IConstants.key;
				String signcode = Md5Utils.md5(md5);
				action = new RechargeAction(info.getId(), info.getLogintoken(),
						payway, String.valueOf(money),signcode);
				module = new RechargeModule();
				presistence = new RechargePresistence(this);
				presistence.setActitons(action);
				presistence.setModule(module);
				presistence.execute();
			} else {
				Toast.makeText(this, "请选择金额", Toast.LENGTH_SHORT).show();
			}

		}

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.upmppay:
			payway = UPMPAY;
			Log.i("hz", "upmppay");
			upmpay.setImageResource(R.drawable.bg_bank_selected_caipiao);
			alipay.setImageResource(0);
			break;
		case R.id.sure_pay:
			if(payway == UPMPAY){
				MyApplication.getInstance().isPaying = true;
			}
			if (!(TextUtils.isEmpty(et_recharge.getText().toString()))) {
				if (payway.equals("")) {
					Toast.makeText(this, "请选择支付方式", Toast.LENGTH_SHORT).show();
				} else {
					startThread();
				}
			} else {
				Toast.makeText(this, "请输入充值金额", Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.alipay:
			payway = ALIPAY;
			Log.i("hz", "alipay");
			upmpay.setImageResource(0);
			alipay.setImageResource(R.drawable.bg_bank_selected_caipiao);
			break;
		case R.id.five:
			money = 500;
			et_recharge.setText(money / 100 + "");
			Button btn = (Button) findViewById(chooseButton);
			btn.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.five;
			btn_five.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);

			break;
		case R.id.ten:
			money = 1000;
			et_recharge.setText(money / 100 + "");
			Button btn1 = (Button) findViewById(chooseButton);
			btn1.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.ten;
			btn_ten.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.thirty:
			money = 3000;
			et_recharge.setText(money / 100 + "");
			Button btn2 = (Button) findViewById(chooseButton);
			btn2.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.thirty;
			btn_thiry.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.fifty:
			money = 5000;
			et_recharge.setText(money / 100 + "");
			Button btn3 = (Button) findViewById(chooseButton);
			btn3.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.fifty;
			btn_fifty.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.hundred:
			money = 10000;
			et_recharge.setText(money / 100 + "");
			Button btn4 = (Button) findViewById(chooseButton);
			btn4.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.hundred;
			btn_hundred
					.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.twohundred:
			money = 20000;
			et_recharge.setText(money / 100 + "");
			Button btn5 = (Button) findViewById(chooseButton);
			btn5.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.twohundred;
			btn_twohundred
					.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.threehundred:
			money = 30000;
			et_recharge.setText(money / 100 + "");
			Button btn6 = (Button) findViewById(chooseButton);
			btn6.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.threehundred;
			btn_threehundred
					.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.fivehundred:
			money = 50000;
			et_recharge.setText(money / 100 + "");
			Button btn7 = (Button) findViewById(chooseButton);
			btn7.setBackgroundResource(R.drawable.bg_charge_btn_noselected_caipiao);
			chooseButton = R.id.fivehundred;
			btn_fivehundred
					.setBackgroundResource(R.drawable.bg_charge_btn_selected_caipiao);
			break;
		case R.id.recharge_money:
			et_recharge.setHint("");
			// 隐藏键盘
			// InputMethodManager imm = (InputMethodManager)
			// getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(0,
			// InputMethodManager.RESULT_SHOWN);
			/*
			 * InputMethodManager imm = (InputMethodManager)
			 * getSystemService(RechargeActivity.INPUT_METHOD_SERVICE);
			 * imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			 */
			break;
		default:
			break;
		}

	}

}
